package cn.rocky.offer;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @version 1.0
 * @description 对称二叉树
 * @author viki
 * @date 2021/7/22 20:57
 */
public class SymmetricBinaryTree {

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }

    /**
     * 判断是否是对称二叉树
     * @param root
     * @return  true  or  false
     */
    public boolean isSymmetric(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        List<Integer> list = new ArrayList<>();
        List<Integer> list1 = new ArrayList<>();
        if (root != null) {
            queue.add(root);
        }
        while(!queue.isEmpty()) {
            TreeNode node = queue.poll();
            if (node.left != null) {
                list.add(node.left.val);
            }
            if (node.right != null) {
                list1.add(node.right.val);
            }
        }
        boolean equals = list.equals(list1);
        return  equals;
    }

    public boolean isSymmetric1(TreeNode root) {
        return root == null ? true : recur(root.left,root.right);
    }

    public boolean recur(TreeNode L,TreeNode R) {
        if(L == null && R == null) {
            return true;
        }
        if(L == null || R == null || L.val != R.val) {
            return false;
        }
        return recur(L.left, R.right) && recur(L.right, R.left);

    }
}
